<!--会员中心 》发布二手交易-->
<template>
  <div style="width: 100%;height: 600px;margin-left: 60px;;
        padding: 20px 20px;box-sizing: border-box;font-size: 17px;text-align: left">
    <h4>发布二手交易</h4>
    <hr style="border: 1px solid rgba(0,0,0,0.1);margin: 10px 0px;">

    <el-dialog
        title="温馨提示"
        style="text-align: left"
        :visible.sync="dialogVisible"
        width="400px"
        :before-close="handleClose">
      <span style="color: #666666;font-size: 12px">
        您需要完成实名认证，认证通过后即可进行信息发布和修改
      </span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="goAuthen">去认证</el-button>
  </span>
    </el-dialog>

    <div style="text-align: left">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="目的" prop="tranDetail">
          <el-select style="width: 100%" v-model="objective" placeholder="请选择交易目的">
            <div style="" v-for="ktem in tranDetail">
              <span style="color: #b1b1b1">{{ ktem.detail }}</span>
              <el-option v-for="item in ktem.tranDetailVOList" :label="item.detail" :value="item.id"/>
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="选择地区" required>
          <el-col :span="11">
            <el-select style="width: 100%" v-model="district1" placeholder="请选择地区" @change="releaseTrans">
              <div v-for="ktem in DistrictList">
                <span style="color: #b1b1b1">{{ktem.district}}</span>
                <el-option v-for="item in ktem.list" :label="item.district" :value="item.id"></el-option>
              </div>
            </el-select>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-select style="width: 100%;position: relative;left: -40px" v-model="district2"
                       placeholder="请选择活动区域">
              <div v-for="item in DistrictListPra">
                <el-option :label="item.district" :value="item.id"/>
              </div>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="ruleForm.contacts"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="详情" prop="detail">
          <el-input type="textarea" v-model="ruleForm.detail"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm)">确认发布</el-button>
          <el-button @click="resetForm('ruleForm')">重新填写</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "MemberRelease",
  data() {
    return {
      dialogVisible: false,
      authentica: {isAuthen: true},
      tranDetail: [],
      DistrictListPra:[],
      DistrictList:[],
      district1:"",
      district2:"",
      objective:"",
      ruleForm: {
        title: '',
        objective: [],
        district: [],
        contacts: '',
        phone: '',
        detail: ''
      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 3, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur'}
        ],
        district1: [
          {required: true, message: '请选择省份', trigger: 'change'}
        ],
        district2: [
          {required: true, message: '请选择市区', trigger: 'change'}
        ],
        contacts: [
          {required: true, message: '请输入联系人', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入联系电话', trigger: 'blur'}
        ],
        detail: [
          {required: true, message: '请输入详细介绍', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleClose(){},
    releaseTrans(){
      for (let i = 0; i < this.DistrictList.length; i++) {
        for (let j=0;j<this.DistrictList[i].list.length;j++){
          if (this.district1==this.DistrictList[i].list[j].id){
            this.DistrictListPra=this.DistrictList[i].list[j].list;
            break;
          }
        }
      }
    },

    goAuthen() {
      this.dialogVisible = false;
      this.$router.push("/member/authentica");
    },

    submitForm(formName) {
      this.ruleForm.objective=this.selectObjective(this.objective)
      this.ruleForm.district.push(this.district1)
      this.ruleForm.district.push(this.district2)
      let jwt=JSON.parse(localStorage.getItem('user')).token;
      console.log(formName)
      this.axios
          .create({
            'headers': {
              'Authorization': jwt
            }
          })
          .post('http://localhost:29000/v1/release/addNew',formName)
          .then((response)=>{
            if (response.data.code==20000){
              this.$message.success("发布成功！")
              this.$router.push('/member/managetrade')
            }else{
              this.$message.error(response.data.message);
            }
          })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    selectObjective(id){
      let tempI,tempJ;
      for (let i = 0; i < this.tranDetail.length; i++) {
        for (let j = 0; j < this.tranDetail[i].tranDetailVOList.length; j++) {
            if (this.tranDetail[i].tranDetailVOList[j].id==id){
              tempI=this.tranDetail[i].id;
              tempJ=id;
            }
        }
      }

      let tempArray=[tempI,tempJ];
      return tempArray;
    },
  },
  mounted() {
    this.user = JSON.parse(localStorage.getItem("user"));
    if (this.user == null) {
      this.$router.push('/login')
      this.$message.error("请重新登录！")
    }

    this.axios.get( 'http://localhost:36000/v1/lookwork/districtAll')
        .then((response) => {
          if (response.data.code == 20000) {
            this.DistrictList = response.data.data;
          }
        })

    this.axios.get( 'http://localhost:29000/v1/release/release-detail')
        .then((response) => {
          if (response.data.code == 20000) {
            this.tranDetail = response.data.data;
          }
        })

    if (!this.authentica.isAuthen) {
      this.dialogVisible = true;
    }
  }
}
</script>

<style scoped>
/deep/ .el-input__inner {
  border-radius: 0px;
}

/deep/ .el-textarea__inner {
  line-height: 1.5;
  font-size: 15px;
}
</style>